$(function(){
    $("#btn").click(function(){
        $("#webcam_box").show();
        InitWebCam();
    });
    $("#close_webcam").click(function(){
         $("#webcam_box").hide();
    });
    chat();
    switchChat();
});

function InitWebCam(){
    $("#webcam").webcam({
        width: 800,
        height: 600,
        mode: "callback",
        swffile: "js/jscam_canvas_only.swf", // canvas only doesn't implement a jpeg encoder, so the file is much smaller
        onTick: function(remain) {

            if (0 == remain) {
                jQuery("#status").text("Cheese!");
            } else {
                jQuery("#status").text(remain + " seconds remaining...");
            }
        },

        onSave: function(data) {

            var col = data.split(";");
        // Work with the picture. Picture-data is encoded as an array of arrays... Not really nice, though =/
        },

        onCapture: function () {
            webcam.save();

        // Show a flash for example
        },

        debug: function (type, string) {
            // Write debug information to console.log() or a div, ...
        },

        onLoad: function () {
        // Page load
            var cams = webcam.getCameraList();
            for(var i in cams) {
                jQuery("#cams").append("<li>" + cams[i] + "</li>");
            }
        }
    });
}

function chat(){
    $("#chat_submit").click(function(){
        var text = $("#input_text").val();
        var html = '<li><span class="username">邓宏：</span><span class="msg">'+text+'</span> </li>';
        $("#chat_content").append(html);
        $("#input_text").val('');
    });
}

function switchChat(){
     $("#function li").click(function(){
         var flag = 0;
         var d = localStorage.getItem("flag");
         if(d){
             flag = d;   
         }
        if(flag == 0){
            $("#function").css("right", "300px");
            $("#chat_text").show();
            localStorage.setItem("flag", 1);
        }else{
            $("#function").css("right", "0");
            $("#chat_text").hide();
            flag = 0;
            localStorage.setItem("flag", 0);
        }
        
    });
}